/* Alison Andrew
11.06.2015
This do file creates a master do file to create main table format for
HI endline report.
In format:

CHANGE COUNT SO THAT IT ONLY COUNTS NON-MISSING OBSERVATIONS

*** AA - 11.12.15 - changed standardisation proceedure so removes tester effects
before non-parametric standardisation
*/


* global directory
*******************

set more off
capture log close


* table to tabulate process outcomes  - do controls

program myttests_process, eclass // program when control for that specifc measure at BL
     version 8
     syntax varlist [if] [in], itt2(varname) itt3(varname)  [ cluster(varname) ]
     tempname cont_mean cont_sd him_mean him_sd beta_him se_him pval_him itt2_mean itt2_sd itt3_mean itt3_sd  beta_2 beta_3 beta_23 pval_1_2 pval_1_3 pval_2_3  b V se_2 se_3 se_23 Num 
	svyset, psu(`cluster')
	
	foreach var of local varlist {
		di "`var'"
		* means by group
			sum `var' if `itt2'==0 & `itt3'==0
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			mat `cont_sd'	=nullmat(`cont_sd'), r(sd)
			
			sum `var' if `itt2'==1 & `itt3'==0
			mat `itt2_mean'	=nullmat(`itt2_mean'), r(mean)
			mat `itt2_sd'	=nullmat(`itt2_sd'), r(sd)
			
			sum `var' if `itt2'==0 & `itt3'==1
			mat `itt3_mean'	=nullmat(`itt3_mean'), r(mean)
			mat `itt3_sd'	=nullmat(`itt3_sd'), r(sd)
			
			count if `var'~=. 
			mat `Num'=nullmat(`Num'), r(N)		
			
		* regression 
			regress `var'  `itt2' `itt3' , cluster(`cluster')
			
	
		* generate adjusted differences in means
		mat `b'=e(b)
		mat `beta_2'	=nullmat(`beta_2'), _b[`itt2']
		mat `beta_3'	=nullmat(`beta_3'), _b[`itt3']
		mat `beta_23'	=nullmat(`beta_23'), _b[`itt3']-_b[`itt2']
		
		mat `V'=e(V)
		mat `se_2'	=nullmat(`se_2'), (`V'[1,1])^0.5
		mat `se_3'	=nullmat(`se_3'), (`V'[2,2])^0.5
		mat `se_23'	=nullmat(`se_23'), (`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
		
		* generate the three p-values
		test `itt2'=0	
		mat `pval_1_2'	=nullmat(`pval_1_2'	), r(p)
						
		test `itt3'=0
		mat `pval_1_3'	=nullmat(`pval_1_3'	), r(p)
				
		test `itt2'=`itt3'	
		mat `pval_2_3'	=nullmat(`pval_2_3'	), r(p)
		
		* pool the two treatment groups
		tempvar him
		gen `him'=(`itt2'==1 | `itt3'==1)
		sum `var' if `him'==1
		mat `him_mean'	=nullmat(`him_mean'), r(mean)
		mat `him_sd'	=nullmat(`him_sd'), r(sd)
		
		regress `var'  `him', cluster(`cluster')
		mat `b'=e(b)
		mat `beta_him'	=nullmat(`beta_him'), _b[`him']
		mat `V'=e(V)
		mat `se_him'	=nullmat(`se_him'), (`V'[1,1])^0.5
		
		test `him'=0
		mat `pval_him'	=nullmat(`pval_him'	), r(p)
		
		}
	
	
	foreach mat in  him_mean him_sd beta_him se_him pval_him cont_mean cont_sd itt2_mean itt2_sd itt3_mean itt3_sd  beta_2 beta_3 beta_23 pval_1_2 pval_1_3 pval_2_3 se_2 se_3 se_23 Num  {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
		 eret local cmd "myttests"
		 foreach mat in   him_mean him_sd beta_him se_him  pval_him cont_mean cont_sd itt2_mean itt2_sd itt3_mean itt3_sd  beta_2 beta_3 beta_23 pval_1_2 pval_1_3 pval_2_3  se_2 se_3 se_23 Num  {
			 eret mat `mat' = ``mat'' 
			 }
	end
		



/** create program to write descriptive table in following format:
Var name | Control mean (adjusted) | Diff 2 (adjusted) | Diff 3 (adjusted) | p-value control vs 2 | p-value control vs 3 | p-value 2 vs 3 
 *******************************************************************************
 *******************************************************************************/
* insert anything into bl option if bl variable exists
**SK CHANGE 31.12.15: TOOK TIP_* OUT SO THAT CAN BE INCLUDED THROUGH CONTROLS ONLY (CAN THEN RUN ANALYSIS WITH AND WITHOUT TRIPLET FE'S IN THE CHILD OUTCOMES FILE WITHOUT CHANGING THIS FILE)

program myttests, eclass // program when control for that specifc measure at BL
     version 8
     syntax varlist [if] [in], itt2(varname) itt3(varname)  [bl(name) controls(varlist) cluster(varname) ]
     tempname cont_mean cont_sd beta_2 beta_3 beta_23 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3 b V se_2 se_3 se_23 Num bl_coeff bl_se bl_pval
	svyset, psu(`cluster')
	preserve	
		cap drop miss_*
	* replace control variables with mean if missing and generate dummy
		foreach x of varlist  `controls'  {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				gen miss_cont_`x'=(`x'==.)
				if `N'~=`n0'+`n1' {
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				}

     foreach var of local varlist {
		di "`var'"
				
		*cap confirm ex "`bl'"=="bl"
		*	if _rc {
		
		if "`bl'"=="" {
		* generate the control mean
		di "no bl equivalent exists"
			sum `var' if `itt2'==0 & `itt3'==0
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			mat `cont_sd'	=nullmat(`cont_sd'), r(sd)
			count if `var'~=. 
			mat `Num'=nullmat(`Num'), r(N)
			
			regress `var'  `itt2' `itt3' `controls' miss_cont_*  dane_*, cluster(`cluster')
			}
		
		else {
		di "bl variable exists"
			* generate the control mean
			sum `var' if `itt2'==0 & `itt3'==0
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			mat `cont_sd'	=nullmat(`cont_sd'), r(sd)
			count if `var'~=.

			mat `Num'=nullmat(`Num'), r(N)

			foreach x of varlist  `var'_0 {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				if `N'~=`n0'+`n1' {
					gen miss_`x'=(`x'==.)
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				
				}
				
			* run regression
			regress `var'  `itt2' `itt3' `var'_0 `controls' miss_cont* miss_`var'_0 dane_*, cluster(`cluster')
			}	
			
			
				 
		* generate adjusted differences in means
		mat `b'=e(b)
		mat `beta_2'	=nullmat(`beta_2'), _b[`itt2']
		mat `beta_3'	=nullmat(`beta_3'), _b[`itt3']
		mat `beta_23'	=nullmat(`beta_23'), _b[`itt3']-_b[`itt2']
		
		mat `V'=e(V)
		mat `se_2'	=nullmat(`se_2'), (`V'[1,1])^0.5
		mat `se_3'	=nullmat(`se_3'), (`V'[2,2])^0.5
		mat `se_23'	=nullmat(`se_23'), (`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
		
		* generate the three p-values
				test `itt2'=0	
				mat `pval_1_2'	=nullmat(`pval_1_2'	), r(p)
				return list
				local sign_itt2=sign(_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), ttail(r(df_r),`sign_itt2'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), 1-normal(`sign_itt2'*sqrt(r(chi2)))
					}				
				test `itt3'=0
				mat `pval_1_3'	=nullmat(`pval_1_3'	), r(p)
				local sign_itt3=sign(_b[`itt3'])
				if r(F)~=. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), ttail(r(df_r),`sign_itt3'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), 1-normal(`sign_itt3'*sqrt(r(chi2)))
					}				
				test `itt2'=`itt3'	
				mat `pval_2_3'	=nullmat(`pval_2_3'	), r(p)
				local sign_itt23=sign(_b[`itt3']-_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), ttail(r(df_r),`sign_itt23'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), 1-normal(`sign_itt23'*sqrt(r(chi2)))
					}
				
		* if bl exists then generate corr with BL value
		if "`bl'"~="" {
			mat `bl_coeff'	=nullmat(`bl_coeff'), _b[`var'_0]
			mat `bl_se'	=nullmat(`bl_se'), (`V'[3,3])^0.5
			test `var'_0=0	
			mat `bl_pval'	=nullmat(`bl_pval'	), r(p)
			}

		}	
	
	if "`bl'"~="" {
		foreach mat in cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 beta_23 Num bl_coeff bl_se bl_pval {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
		 *eret local cmd "myttests"
		 foreach mat in  cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3  pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 beta_23 Num bl_coeff bl_se bl_pval{
			 eret mat `mat' = ``mat'' 
		 }
		 }
	
	if "`bl'"=="" {
		foreach mat in cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 beta_23 Num  {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
		 *eret local cmd "myttests"
		 foreach mat in  cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3  se_2 se_3 se_23 beta_23 Num {
			 eret mat `mat' = ``mat'' 
		 }
	}
	drop miss*
	restore
end	




/** create program to write descriptive table in following format:
Var name | Control mean (adjusted) | Diff 2 (adjusted) | Diff 3 (adjusted) | p-value control vs 2 | p-value control vs 3 | p-value 2 vs 3 
 *******************************************************************************
 *******************************************************************************/
* insert anything into bl option if bl variable exists

program post_r

program myttests_rw, eclass // program when control for that specifc measure at BL
     version 8
     syntax varlist [if] [in], itt2(varname) itt3(varname) setname(name)  [bl(name) controls(varlist) cluster(varname) ]
     tempname cont_mean cont_sd beta_2 beta_3 beta_23 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3 b V se_2 se_3 se_23 Num bl_coeff bl_se bl_pval
	svyset, psu(`cluster')
		preserve
		cap drop miss_*
	* replace control variables with mean if missing and generate dummy
		foreach x of varlist  `controls'  {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				gen miss_cont_`x'=(`x'==.)
				if `N'~=`n0'+`n1' {
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				}

     foreach var of local varlist {
		di "`var'"
				
		*cap confirm ex "`bl'"=="bl"
		*	if _rc {
		
		if "`bl'"=="" {
		* generate the control mean
		di "no bl equivalent exists"
			sum `var' if `itt2'==0 & `itt3'==0
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			mat `cont_sd'	=nullmat(`cont_sd'), r(sd)
			count if `var'~=. 
			mat `Num'=nullmat(`Num'), r(N)
			
			regress `var'  `itt2' `itt3' `controls' miss_cont_* trip_* dane_*, cluster(`cluster')
			}
		
		else {
		di "bl variable exists"
			* generate the control mean
			sum `var' if `itt2'==0 & `itt3'==0
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			mat `cont_sd'	=nullmat(`cont_sd'), r(sd)
			count if `var'~=.

			mat `Num'=nullmat(`Num'), r(N)

			foreach x of varlist  `var'_0 {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				if `N'~=`n0'+`n1' {
					gen miss_`x'=(`x'==.)
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				
				}
				
			* run regression
			regress `var'  `itt2' `itt3' `var'_0 `controls' miss_cont* miss_`var'_0 trip_* dane_*, cluster(`cluster')
			}	
			
			
				 
		* generate adjusted differences in means
		mat `b'=e(b)
		mat `beta_2'	=nullmat(`beta_2'), _b[`itt2']
		mat `beta_3'	=nullmat(`beta_3'), _b[`itt3']
		mat `beta_23'	=nullmat(`beta_23'), _b[`itt3']-_b[`itt2']
		
		mat `V'=e(V)
		mat `se_2'	=nullmat(`se_2'), (`V'[1,1])^0.5
		mat `se_3'	=nullmat(`se_3'), (`V'[2,2])^0.5
		mat `se_23'	=nullmat(`se_23'), (`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
		
		* generate the three p-values
				test `itt2'=0	
				mat `pval_1_2'	=nullmat(`pval_1_2'	), r(p)
				return list
				local sign_itt2=sign(_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), ttail(r(df_r),`sign_itt2'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), 1-normal(`sign_itt2'*sqrt(r(chi2)))
					}				
				test `itt3'=0
				mat `pval_1_3'	=nullmat(`pval_1_3'	), r(p)
				local sign_itt3=sign(_b[`itt3'])
				if r(F)~=. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), ttail(r(df_r),`sign_itt3'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), 1-normal(`sign_itt3'*sqrt(r(chi2)))
					}				
				test `itt2'=`itt3'	
				mat `pval_2_3'	=nullmat(`pval_2_3'	), r(p)
				local sign_itt23=sign(_b[`itt3']-_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), ttail(r(df_r),`sign_itt23'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), 1-normal(`sign_itt23'*sqrt(r(chi2)))
					}
				
		* if bl exists then generate corr with BL value
		if "`bl'"~="" {
			mat `bl_coeff'	=nullmat(`bl_coeff'), _b[`var'_0]
			mat `bl_se'	=nullmat(`bl_se'), (`V'[3,3])^0.5
			test `var'_0=0	
			mat `bl_pval'	=nullmat(`bl_pval'	), r(p)
			}

		}	
	
	if "`bl'"~="" {
		foreach mat in cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 beta_23 Num bl_coeff bl_se bl_pval {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
		 eret local cmd "myttests"
		 foreach mat in  cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3  pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 beta_23 Num bl_coeff bl_se bl_pval{
			 eret mat `mat' = ``mat'' 
		 }
		 }
	
	if "`bl'"=="" {
		foreach mat in cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 beta_23 Num  {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
		 eret local cmd "myttests"
		 foreach mat in  cont_mean cont_sd beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3  se_2 se_3 se_23 beta_23 Num {
			 eret mat `mat' = ``mat'' 
		 }
	}
	drop miss*
	restore
end	

* ANOTHER TABLE FOR BINARY OUTCOMES

program bin_setup
	syntax varlist
	foreach var of local varlist {
	cap  drop miss_* p_* s_* d_*

		di "`var'"
		cap drop p_`var' d_`var' s_`var'
		gen p_`var'=`var' 
		label var p_`var' "Unadjusted proportion" 
		gen d_`var'=`var' 
		label var d_`var' "Adjsuted difference" 		
		gen s_`var'=`var' 
		label var s_`var' "\textit{(s.e.)"

		}
end	


program myttests_bin, eclass // program when control for that specifc measure at BL
     version 13.0
     syntax varlist [if] [in], itt2(varname) itt3(varname)  [bl(name) controls(varlist) cluster(varname) ]
     tempname cont_prop diff_2 diff_3 diff_23 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3  b V se_2 se_3 se_23 Num diff_bl se_bl pval_bl b_vs V_vs
	svyset, psu(`cluster')
	preserve
	cap drop miss_*
		* replace control variables with mean if missing and generate dummy
	* replace control variables with mean if missing and generate dummy
		foreach x of varlist  `controls'  {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				gen miss_cont_`x'=(`x'==.)
				if `N'~=`n0'+`n1' {
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				local miss ""
				foreach y of varlist miss_* {
					local miss "`miss' `y'"
				}
				}

				
     foreach var of local varlist {
		di "`var'"
				
			sum `var' if `itt2'==0 & `itt3'==0
			mat `cont_prop'	=nullmat(`cont_prop'), r(mean)
			count if `var'~=.
			mat `Num'=nullmat(`Num'), r(N)

			* logistic regression
			if "`bl'"=="" { 				
				local missing_dummies ""
				foreach y in `miss' {
					sum `y' 
					if r(V) ~=0 {
						local missing_dummies "`missing_dummies' `y'"
						}
					}			
				}
			
			if "`bl'"~="" {
					foreach x of varlist  `var'_0 {
						count if `x'~=.
						local N=r(N)
						count if `x'==1
						local n1=r(N)
						count if `x'==0
						local n0=r(N)
						if `N'~=`n0'+`n1' {
							gen miss_`x'=(`x'==.)
							sum `x'
							replace `x'=r(mean) if `x'==. 
							local miss "`miss' miss_`var'_0"
							}
						}
					
				local missing_dummies ""
				di "`miss'"
				d miss*
				foreach y in `miss' {
					sum `y' 
					if r(sd) ~=0 {
						local missing_dummies "`missing_dummies' `y'"
						}
					}
				}
				
			sum `var' if `itt2'==1 // does treatment perfectly predict success? 
			if r(sd)==0 {
				local predict=1
				}
			if r(sd)~=0{
				local predict=0
				}
			
			sum `var' if `itt3'==1 	
			if r(sd)==0 {
				local predict=1
				}
			if r(sd)~=0{
				if `predict'==0 {
					local predict=0
					}
				if `predict'==1 {
					local predict=1
					}
				}
			
			sum `var' if `itt3'==0 & `itt2'==0 //  does control perfectly predict failure
			if r(sd)==0 {
				local predict=1
				}
			if r(sd)~=0{
				if `predict'==0 {
					local predict=0
					}
				if `predict'==1 {
					local predict=1
					}
				}
				
			di "predict (1 if treatment perfectly predicts outcome) --- `predict'"	
			
			if `predict'==0 { 
				* generate the three p-values
				if "`bl'"~="" {
					logit `var'  `itt2' `itt3' `var'_0 `controls'  dane_* , cluster(`cluster')  
					}
				if "`bl'"=="" {
					logit `var'  `itt2' `itt3'  `controls' dane_* , cluster(`cluster')  
					}					
				
				test `itt2'=0	
				mat `pval_1_2'	=nullmat(`pval_1_2'	), r(p)
				return list
				local sign_itt2=sign(_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), ttail(r(df_r),`sign_itt2'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), 1-normal(`sign_itt2'*sqrt(r(chi2)))
					}				
				test `itt3'=0
				mat `pval_1_3'	=nullmat(`pval_1_3'	), r(p)
				local sign_itt3=sign(_b[`itt3'])
				if r(F)~=. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), ttail(r(df_r),`sign_itt3'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), 1-normal(`sign_itt3'*sqrt(r(chi2)))
					}				
				test `itt2'=`itt3'	
				mat `pval_2_3'	=nullmat(`pval_2_3'	), r(p)
				local sign_itt23=sign(_b[`itt3']-_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), ttail(r(df_r),`sign_itt23'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), 1-normal(`sign_itt23'*sqrt(r(chi2)))
					}
				
				di "**************************"
				mat list `pval1s_1_2'
		
				if "`bl'"~="" {
					test `var'_0=0	
					mat `pval_bl'	=nullmat(`pval_bl'	), r(p)
					}
			
				* generate adjusted differences in proportions
				if "`bl'"~="" {
					sum `var'_0
					if r(sd)<0.03 {
						local var_block=1
						mat `diff_bl'	=nullmat(`diff_bl'), .
						mat `se_bl'	=nullmat(`se_bl'), .
						}
					if r(sd)>0.03 {
						local var_block=0
						}
					}
				if "`bl'"=="" {
					local var_block=0
					}
				di "******************* BL VARBLOCK `bl'	 - `var_block'"
				if "`bl'"~="" & `var_block'==0 {
					logit `var'  i.`itt2' i.`itt3' i.`var'_0 `controls' dane_* , cluster(`cluster')  
					}
				if "`bl'"=="" | `var_block'==1 {
					logit `var'  i.`itt2' i.`itt3'  `controls' dane_*, cluster(`cluster')  
					}	
				margins `itt2'#`itt3'
				margins `itt2'#`itt3', pwcompare
				mat `b_vs'=r(b_vs)
				mat list `b_vs'
				mat `diff_2'	=nullmat(`diff_2'),`b_vs'[1,2]
				mat `diff_3'	=nullmat(`diff_3'), `b_vs'[1,1]
				mat `diff_23'	=nullmat(`diff_23'), -1*`b_vs'[1,4]
				di "**********************"
				mat list `diff_2'
				mat `V_vs' =r(V_vs)
				mat `se_2'	=nullmat(`se_2'),`V_vs'[2,2]^0.5
				mat `se_3'	=nullmat(`se_3'),`V_vs'[1,1]^0.5
				mat `se_23'	=nullmat(`se_23'),`V_vs'[4,4]^0.5
				
				* if bl exists then generate corr with BL value
				if "`bl'"~="" & `var_block'==0 {
					margins `var'_0, pwcompare
					mat `b_vs'=r(b_vs)
					mat `diff_bl'	=nullmat(`diff_bl'), `b_vs'[1,1]
					mat `V_vs' =r(V_vs)
					mat `se_bl'	=nullmat(`se_bl'), `V_vs'[1,1]^0.5
					}
				
				}
				
			if `predict'==1 {
				* use linear model if perfectly predicts
				if "`bl'"~="" {
					regress `var'  `itt2' `itt3' `var'_0 `controls' dane_*, cluster(`cluster')
					}
				if "`bl'"=="" {
					regress `var'  `itt2' `itt3' `controls' dane_*, cluster(`cluster')
					}
				* generate the three p-values
				test `itt2'=0	
				mat `pval_1_2'	=nullmat(`pval_1_2'	), r(p)
				return list
				local sign_itt2=sign(_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), ttail(r(df_r),`sign_itt2'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_2'	=nullmat(`pval1s_1_2'), 1-normal(`sign_itt2'*sqrt(r(chi2)))
					}				
				test `itt3'=0
				mat `pval_1_3'	=nullmat(`pval_1_3'	), r(p)
				local sign_itt3=sign(_b[`itt3'])
				if r(F)~=. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), ttail(r(df_r),`sign_itt3'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_1_3'	=nullmat(`pval1s_1_3'), 1-normal(`sign_itt3'*sqrt(r(chi2)))
					}				
				test `itt2'=`itt3'	
				mat `pval_2_3'	=nullmat(`pval_2_3'	), r(p)
				local sign_itt23=sign(_b[`itt3']-_b[`itt2'])
				if r(F)~=. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), ttail(r(df_r),`sign_itt23'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval1s_2_3'	=nullmat(`pval1s_2_3'), 1-normal(`sign_itt23'*sqrt(r(chi2)))
					}
				
				if "`bl'"~="" {
					test `var'_0=0	
					mat `pval_bl'	=nullmat(`pval_bl'	), r(p)
					}
			
				* adjusted difference
				mat `b'=e(b)
				mat `diff_2'	=nullmat(`diff_2'), _b[`itt2']
				mat `diff_3'	=nullmat(`diff_3'), _b[`itt3']
				mat `diff_23'	=nullmat(`diff_23'), _b[`itt3']-_b[`itt2']
	
					
				mat `V'=e(V)
				mat `se_2'	=nullmat(`se_2'), (`V'[1,1])^0.5
				mat `se_3'	=nullmat(`se_3'), (`V'[2,2])^0.5
				mat `se_23'	=nullmat(`se_23'), (`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
				
				* if bl exists then generate corr with BL value
				if "`bl'"~="" {
					mat `diff_bl'	=nullmat(`diff_bl'), _b[`var'_0]
					mat `se_bl'	=nullmat(`se_bl'), (`V'[3,3])^0.5
					test ``var'_0'=0	
					mat `pval_bl'	=nullmat(`pval_bl'	), r(p)
					}		
				}
					
		}
		
		

		
	if "`bl'"~="" {
		foreach mat in cont_prop diff_2 diff_3 diff_23 pval_1_2 pval_1_3 pval_2_3  pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 Num diff_bl se_bl pval_bl {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
		 eret local cmd "myttests"
		 foreach mat in  cont_prop diff_2 diff_3 diff_23 pval_1_2 pval_1_3 pval_2_3  pval1s_1_2 pval1s_1_3 pval1s_2_3 se_2 se_3 se_23 Num diff_bl se_bl pval_bl {
			 eret mat `mat' = ``mat'' 
		 }
		 }
	
	if "`bl'"=="" {
		foreach mat in cont_prop diff_2 diff_3 diff_23 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3  se_2 se_3 se_23 Num    {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
		 eret local cmd "myttests"
		 foreach mat in  cont_prop diff_2 diff_3 diff_23 pval_1_2 pval_1_3 pval_2_3 pval1s_1_2 pval1s_1_3 pval1s_2_3  se_2 se_3 se_23 Num  {
			 eret mat `mat' = ``mat'' 
		 }
	}
	
	 restore
end	



* HETEROGENEITY ANALYSIS
*************************

program hetero_setup
	syntax varlist, hetero(varname) label0(string) label1(string)
	foreach var of local varlist {
		di "`var'"
		cap drop h0_`hetero'_`var' h1_`hetero'_`var' p_`hetero'_`var'
		gen h0_`hetero'_`var'=`var' if `hetero'==0
		label var h0_`hetero'_`var' "`label0'" 
		gen h1_`hetero'_`var'=`var' if `hetero'==1
		label var h1_`hetero'_`var' "`label1'" 
		
		gen p_`hetero'_`var'=.
		label var p_`hetero'_`var' "\textit{p-value}"
		}
end		
	
	

program hetero, eclass
     version 8
     syntax varlist [if] [in], itt2(varname) itt3(varname) hvars(varlist) sided(integer) [bl(name) controls(varlist) cluster(varname) ]
     marksample touse
     markout `touse' `by' 
     tempname cont_mean beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 b V se_2 se_3 Num itt2Xhet itt3Xhet beta_23 se_23
	svyset, psu(`cluster')
 
	preserve
 		cap drop miss_*
		* replace control variables with mean if missing and generate dummy
		foreach x of varlist  `controls'  {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				gen miss_cont_`x'=(`x'==.)
				if `N'~=`n0'+`n1' {
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				}
				
	foreach var of local varlist {
		di "**************`var'"
		
		* empty row for each matrix
		***************************
		foreach mat in cont_mean beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 se_2 se_3  beta_23 se_23 Num {
			mat ``mat''=nullmat(``mat''),.
			}
		


			
		foreach hetero in `hvars' {
		
		* het==0
		*********
		if "`bl'"=="" {
			sum h0_`hetero'_`var' if `itt2'==0 & `itt3'==0 
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			count if h0_`hetero'_`var'~=.  
			mat `Num'=nullmat(`Num'), r(N)
			
			regress h0_`hetero'_`var'  `itt2' `itt3' `controls' miss_*  dane_*, cluster(`cluster')
			}
		
		else {
			sum h0_`hetero'_`var' if `itt2'==0 & `itt3'==0 
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			count if h0_`hetero'_`var'~=.
			mat `Num'=nullmat(`Num'), r(N)
			
			foreach x of varlist  `var'_0 {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				if `N'~=`n0'+`n1' {
					cap gen miss_`x'=(`x'==.)
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				}
			
			regress h0_`hetero'_`var' `itt2' `itt3' `var'_0   `controls' miss_*  dane_*, cluster(`cluster')
			}
			
			* adjusted differences in means and ses
			mat `beta_2'	=nullmat(`beta_2'), _b[`itt2'] 
			mat `beta_3'	=nullmat(`beta_3'), _b[`itt3']
			mat `beta_23'	=nullmat(`beta_23'), _b[`itt3']-_b[`itt2']	
			mat `V'=e(V)
			mat `se_2'	=nullmat(`se_2'), (`V'[1,1])^0.5
			mat `se_3'	=nullmat(`se_3'), (`V'[2,2])^0.5
			mat `se_23'	=nullmat(`se_23'), (`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
			
			* generate the three p-values
			if "`sided'"=="2" {
				di "************************* TWO SIDED TEST***********************"
				test `itt2'=0	
				mat `pval_1_2'	=nullmat(`pval_1_2'	), r(p)
				test `itt3'=0	
				mat `pval_1_3'	=nullmat(`pval_1_3'	), r(p)
				test `itt2'=`itt3'	
				mat `pval_2_3'	=nullmat(`pval_2_3'	), r(p)
				}
			
			if "`sided'"=="1" {
				test `itt2'=0	
				return list
				local sign_itt2=sign(_b[`itt2'])
				if r(F)~=. {
					mat `pval_1_2'	=nullmat(`pval_1_2'), ttail(r(df_r),`sign_itt2'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval_1_2'	=nullmat(`pval_1_2'), 1-normal(`sign_itt2'*sqrt(r(chi2)))
					}				
				test `itt3'=0
				local sign_itt3=sign(_b[`itt3'])
				if r(F)~=. {
					mat `pval_1_3'	=nullmat(`pval_1_3'), ttail(r(df_r),`sign_itt3'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval_1_3'	=nullmat(`pval_1_3'), 1-normal(`sign_itt3'*sqrt(r(chi2)))
					}				
				test `itt2'=`itt3'	
				local sign_itt23=sign(_b[`itt3']-_b[`itt2'])
				if r(F)~=. {
					mat `pval_2_3'	=nullmat(`pval_2_3'), ttail(r(df_r),`sign_itt23'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval_2_3'	=nullmat(`pval_2_3'), 1-normal(`sign_itt23'*sqrt(r(chi2)))
					}
				}
				
		di "***1***"
		mat list `pval_1_2'
				
		* het==1
		*********
		if "`bl'"=="" {
			sum h1_`hetero'_`var' if `itt2'==0 & `itt3'==0 
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			count if h1_`hetero'_`var'~=.  
			mat `Num'=nullmat(`Num'), r(N)
			
			regress h1_`hetero'_`var'  `itt2' `itt3' `controls' miss_*  dane_*, cluster(`cluster')
			}
		
		else {
			sum h1_`hetero'_`var' if `itt2'==0 & `itt3'==0 
			mat `cont_mean'	=nullmat(`cont_mean'), r(mean)
			count if h1_`hetero'_`var'~=.
			mat `Num'=nullmat(`Num'), r(N)
			
			regress h1_`hetero'_`var' `itt2' `itt3' `var'_0   `controls' miss_*  dane_*, cluster(`cluster')
			}
			
			* adjusted differences in means and ses
			mat `beta_2'	=nullmat(`beta_2'), _b[`itt2'] 
			mat `beta_3'	=nullmat(`beta_3'), _b[`itt3']
			mat `beta_23'	=nullmat(`beta_23'), _b[`itt3']-_b[`itt2']	
			mat `V'=e(V)
			mat `se_2'	=nullmat(`se_2'), (`V'[1,1])^0.5
			mat `se_3'	=nullmat(`se_3'), (`V'[2,2])^0.5
			mat `se_23'	=nullmat(`se_23'), (`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
			
			* generate the three p-values
			if "`sided'"=="2" {
				test `itt2'=0	
				mat `pval_1_2'	=nullmat(`pval_1_2'	), r(p)
				test `itt3'=0	
				mat `pval_1_3'	=nullmat(`pval_1_3'	), r(p)
				test `itt2'=`itt3'	
				mat `pval_2_3'	=nullmat(`pval_2_3'	), r(p)
				}
			
				di "***2***"
				mat list `pval_1_2'
		
			if "`sided'"=="1" {
				test `itt2'=0	
				return list
				local sign_itt2=sign(_b[`itt2'])
				if r(F)~=. {
					mat `pval_1_2'	=nullmat(`pval_1_2'), ttail(r(df_r),`sign_itt2'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval_1_2'	=nullmat(`pval_1_2'), 1-normal(`sign_itt2'*sqrt(r(chi2)))
					}				
				test `itt3'=0
				local sign_itt3=sign(_b[`itt3'])
				if r(F)~=. {
					mat `pval_1_3'	=nullmat(`pval_1_3'), ttail(r(df_r),`sign_itt3'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval_1_3'	=nullmat(`pval_1_3'), 1-normal(`sign_itt3'*sqrt(r(chi2)))
					}				
				test `itt2'=`itt3'	
				local sign_itt23=sign(_b[`itt3']-_b[`itt2'])
				if r(F)~=. {
					mat `pval_2_3'	=nullmat(`pval_2_3'), ttail(r(df_r),`sign_itt23'*sqrt(r(F)))
					}
				if r(F)==. {
					mat `pval_2_3'	=nullmat(`pval_2_3'), 1-normal(`sign_itt23'*sqrt(r(chi2)))
					}
					di "***3***"
				mat list `pval_1_2'	
				}
			
		* row testing for difference by heterogeneity
		*********************************************
		if "`bl'"=="" {
			*generate interaction term
			tempvar itt2Xhet_`hetero'
			gen `itt2Xhet_`hetero''=`itt2'*`hetero'
			tempvar itt3Xhet_`hetero'
			gen `itt3Xhet_`hetero''=`itt3'*`hetero'
			
			regress `var'  `itt2Xhet_`hetero'' `itt3Xhet_`hetero''  `hetero' `itt2' `itt3' `controls'  dane_* miss_*, cluster(`cluster')
			test `itt2Xhet_`hetero''=0
			mat `beta_2'	=nullmat(`beta_2'), r(p)
			test `itt3Xhet_`hetero''=0
			mat `beta_3'	=nullmat(`beta_3'), r(p)
			test `itt3Xhet_`hetero''=`itt2Xhet_`hetero''
			mat `beta_23'	=nullmat(`beta_23'), r(p)
			}
			
		 else {
			*generate interaction term
			tempvar itt2Xhet_`hetero'
			gen `itt2Xhet_`hetero''=`itt2'*`hetero'
			tempvar itt3Xhet_`hetero'
			gen `itt3Xhet_`hetero''=`itt3'*`hetero'
			
			regress `var'  `itt2Xhet_`hetero'' `itt3Xhet_`hetero''  `hetero' `itt2' `itt3'  `var'_0 `controls'  dane_* miss_*, cluster(`cluster')
			test `itt2Xhet_`hetero''=0
			mat `beta_2'	=nullmat(`beta_2'), r(p)
			test `itt3Xhet_`hetero''=0
			mat `beta_3'	=nullmat(`beta_3'), r(p)
			test `itt3Xhet_`hetero''=`itt2Xhet_`hetero''
			mat `beta_23'	=nullmat(`beta_23'), r(p)
			}		
	
	di "******************HERE************"
	mat list `pval_1_2'
	* blank here for other matrices
			foreach mat in cont_mean  pval_1_2 pval_1_3 pval_2_3 se_2 se_3 se_23 Num {
				mat ``mat''=nullmat(``mat''),.
				
				}
				mat list `pval_1_2'
	
		}
		}
			
	*generate labels list 
     local label ""
	 foreach var of local varlist {
	 	local label "`label' `var'"

 		foreach hetero in `hvars' {
			local label "`label' h0_`hetero'_`var'"
			local label "`label' h1_`hetero'_`var'"
			local label "`label' p_`hetero'_`var'"
			}
			
		}
			
	foreach mat in cont_mean beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3 se_2 se_3 se_23 beta_23 Num {
		mat colname ``mat'' = `label'
		}
		*/
		
     eret local cmd "myttests"
     foreach mat in  cont_mean beta_2 beta_3 pval_1_2 pval_1_3 pval_2_3  se_2 se_3 se_23 beta_23  Num {
         eret mat `mat' = ``mat'' 
		}
		restore
	
end	
	
	





****** TABLES ********************	

*** ONE SIDED TESTS
program table_1s // previously table_raq
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" " . se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" " . se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
end

program table_std_1s // previously table_std_raq_nobl
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
end

* TWO SIDED TESTS
program table_2s 
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" " . se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" " . se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
end

program table_std_2s // previously table_std_raq_2s
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) . .") 
end

* RW TABLES
program table_std_RW_1s // previously table_std_RW_nobl
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) pval1s_1_2(par([ ]))  se_3(fmt(3) par) pval1s_1_3(par([ ])) se_23(fmt(3) par) pval1s_2_3(par([ ]))  .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) pval1s_1_2(par([ ]))  se_3(fmt(3) par) pval1s_1_3(par([ ])) se_23(fmt(3) par) pval1s_2_3(par([ ]))  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) pval1s_1_2(par([ ])) se_3(fmt(3) par) pval1s_1_3(par([ ])) se_23(fmt(3) par) pval1s_2_3(par([ ]))  .") 
end

program table_std_RW_2s // table_std_RW_nobl_2s
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) pval_1_2(par([ ]))  se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ]))  .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) pval_1_2(par([ ]))  se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ]))  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) pval_1_2(par([ ])) se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ]))  .") 
end

program table_RW_2s 
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3))  beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) pval_1_2(par([ ]))  se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ]))  .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3))  beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))  Num(fmt(0))" " . se_2(fmt(3) par) pval_1_2(par([ ]))  se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ]))  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("cont_mean(fmt(3))  beta_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) beta_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) beta_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))  Num(fmt(0))" ". se_2(fmt(3) par) pval_1_2(par([ ])) se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ]))  .") 
end

program table_bin_RW_2s // previously table_bin_nobl_RW
syntax, filename(name) 
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N")  ///
	cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) diff_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) diff_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))   Num(fmt(0))" ". se_2(fmt(3) par) pval_1_2(par([ ]))  se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ])) .") 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") /// 
	cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) diff_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) diff_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))   Num(fmt(0))" ". se_2(fmt(3) par) pval_1_2(par([ ]))  se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ])) .") 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N")  ///
	cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(RW_pvalue_2)) RW_pvalue_2(fmt(3)) diff_3(fmt(3)star pvalue(RW_pvalue_3)) RW_pvalue_3(fmt(3)) diff_23(fmt(3)star pvalue(RW_pvalue_23)) RW_pvalue_23(fmt(3))   Num(fmt(0))" ". se_2(fmt(3) par) pval_1_2(par([ ]))  se_3(fmt(3) par) pval_1_3(par([ ])) se_23(fmt(3) par) pval_2_3(par([ ])) .") 
end

* HETEROGENEITY TABLES
program table_het_2s // previously table_het_raq
syntax, filename(name)
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs nogaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
end

program table_het_1s // previously table_het_raq
syntax, filename(name)
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs nogaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
end


* TABLE PROCESSES
program table_process
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM comb.   mean (SD)""Difference (SE)   HIM comb.-Control" "p-value"  "HIM only   mean (SD)""Difference (SE)   HIM-Control" "p-value" "HIM+FE   mean (SD)" "Difference (SE)   HIM+FE-Control" "p-value" "Difference (SE)   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) him_mean beta_him(fmt(3) star pvalue(pval_him)) pval_him itt2_mean beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) itt3_mean beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) him_sd(fmt(3) par) se_him(fmt(3) par) . itt2_sd(fmt(3) par) se_2(fmt(3) par) . itt3_sd(fmt(3) par)  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM comb.   mean (SD)""Difference (SE)   HIM comb.-Control" "p-value"  "HIM only   mean (SD)""Difference (SE)   HIM-Control" "p-value" "HIM+FE   mean (SD)" "Difference (SE)   HIM+FE-Control" "p-value" "Difference (SE)   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) him_mean beta_him(fmt(3) star pvalue(pval_him)) pval_him itt2_mean beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) itt3_mean beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) him_sd(fmt(3) par) se_him(fmt(3) par) . itt2_sd(fmt(3) par) se_2(fmt(3) par) . itt3_sd(fmt(3) par)  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM comb.   mean (SD)""Difference (SE)   HIM comb.-Control" "p-value"  "HIM only   mean (SD)""Difference (SE)   HIM-Control" "p-value" "HIM+FE   mean (SD)" "Difference (SE)   HIM+FE-Control" "p-value" "Difference (SE)   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) him_mean beta_him(fmt(3) star pvalue(pval_him)) pval_him itt2_mean beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) itt3_mean beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) him_sd(fmt(3) par) se_him(fmt(3) par) . itt2_sd(fmt(3) par) se_2(fmt(3) par) . itt3_sd(fmt(3) par)  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
end

program table_process2
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM only   mean (SD)""Difference (SE)   HIM-Control" "p-value"  "HIM+FE   mean (SD)" "Difference (SE)   HIM+FE-Control" "p-value" "Difference (SE)   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) itt2_mean beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) itt3_mean beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) itt2_sd(fmt(3) par) se_2(fmt(3) par) . itt3_sd(fmt(3) par)  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM only   mean (SD)""Difference (SE)   HIM-Control" "p-value" "HIM+FE   mean (SD)" "Difference (SE)   HIM+FE-Control" "p-value" "Difference (SE)   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) itt2_mean beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) itt3_mean beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) itt2_sd(fmt(3) par) se_2(fmt(3) par) . itt3_sd(fmt(3) par)  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM only   mean (SD)""Difference (SE)   HIM-Control" "p-value" "HIM+FE   mean (SD)" "Difference (SE)   HIM+FE-Control" "p-value" "Difference (SE)   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) itt2_mean beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) itt3_mean beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) itt2_sd(fmt(3) par) se_2(fmt(3) par) . itt3_sd(fmt(3) par)  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
end

* BALANCE TABLE
program table_balance
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM only   mean (SD)"  "HIM+FE   mean (SD)" "HIM vs. Control   p-value""HIM+FE vs. Control   p-value" "HIM+FE vs. HIM   p-value""N") cells("cont_mean(fmt(3)) itt2_mean(star pvalue(pval_1_2)) itt3_mean(fmt(3)star pvalue(pval_1_3)) pval_1_2(fmt(3))  pval_1_3(fmt(3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) itt2_sd(fmt(3) par)  itt3_sd(fmt(3) par) . .  .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM only   mean (SD)"  "HIM+FE   mean (SD)" "HIM vs. Control   p-value""HIM+FE vs. Control   p-value" "HIM+FE vs. HIM   p-value""N") cells("cont_mean(fmt(3)) itt2_mean(star pvalue(pval_1_2)) itt3_mean(fmt(3)star pvalue(pval_1_3)) pval_1_2(fmt(3))  pval_1_3(fmt(3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) itt2_sd(fmt(3) par)  itt3_sd(fmt(3) par)  . .  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean (SD)" "HIM only   mean (SD)"  "HIM+FE   mean (SD)" "HIM vs. Control   p-value""HIM+FE vs. Control   p-value" "HIM+FE vs. HIM   p-value""N") cells("cont_mean(fmt(3)) itt2_mean(star pvalue(pval_1_2)) itt3_mean(fmt(3)star pvalue(pval_1_3)) pval_1_2(fmt(3))  pval_1_3(fmt(3)) pval_2_3(fmt(3))  Num(fmt(0))" "cont_sd(fmt(3) par) itt2_sd(fmt(3) par)  itt3_sd(fmt(3) par) . .  .") 
end

* TABLE FOR BINARY DATA
program table_bin_2s // previously table_bin_nobl
syntax, filename(name) 
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))   Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))   Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))   Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . .") 
end




/*
program table_bin
syntax, filename(name) 
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3))  diff_bl(fmt(3))  pval1s_bl Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . se_bl(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3))  diff_bl(fmt(3))  pval1s_bl Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . se_bl(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3))  diff_bl(fmt(3))  pval1s_bl Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . se_bl(fmt(3) par) . .") 
end

program table_bin_2s
syntax, filename(name) 
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  diff_bl(fmt(3))  pval_bl Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . se_bl(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  diff_bl(fmt(3))  pval_bl Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . se_bl(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control proportion"  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N")  cells("cont_prop(fmt(3)) diff_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) diff_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) diff_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  diff_bl(fmt(3))  pval_bl Num(fmt(0))" ". se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . se_bl(fmt(3) par) . .") 
end
*/



/* PROGRAM TO DRAW TABLE FOR STANDARDISED DATA (NO CONTROL MEAN COLUMN NEEDED)
*****************************************************************************
*****************************************************************************
program table_std
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM""Adjusted difference   HIM+FE"  "P-value   control vs. HIM" "P-value   control vs. HIM+FE" "P-value   HIM vs. HIM+FE""N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_2(fmt(3)) pval1s_1_3(fmt(3)) pval1s_2_3(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) se_3(fmt(3) par) . . . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference - HIM" "Adjusted difference - HIM+FE"  "P-value - control vs. HIM"  "P-value - control vs. HIM+FE"  "P-value - HIM vs. HIM+FE" "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_2(fmt(3)) pval1s_1_3(fmt(3)) pval1s_2_3(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) se_3(fmt(3) par) . . . .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference - HIM" "Adjusted difference - HIM+FE"  "P-value - control vs. HIM"  "P-value - control vs. HIM+FE"  "P-value - HIM vs. HIM+FE" "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_2(fmt(3)) pval1s_1_3(fmt(3)) pval1s_2_3(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) se_3(fmt(3) par) . . . .") 
end

program table_std_raq
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
end

program table_std_raq_2s
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"  "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "Baseline   measure" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) bl_coeff(fmt(3))  bl_pval Num(fmt(0))" "se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) . bl_se(fmt(3) par) . .") 
end

program table_std_raq_nobl
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval1s_1_2)) pval1s_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval1s_1_3)) pval1s_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval1s_2_3)) pval1s_2_3(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
end

program table_std_raq_nobl_2s
syntax, filename(name)
esttab using "$output/`filename'.tex",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.rtf",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) .  se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
esttab using "$output/`filename'.csv",   nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"   "N") cells("beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2(fmt(3)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3)star pvalue(pval_2_3)) pval_2_3(fmt(3))  Num(fmt(0))" "se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(fmt(3) par) .  .") 
end

* TABLES TO DRAW HETEROGENEITY ANALYSIS (NO SES IN BRACKETS)
*************************************************************
*************************************************************
program table_het_raq
syntax, filename(name)
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"  "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs nogaps nolines nonumbers label collabel( "Control mean" "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells("cont_mean(fmt(3)) beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" ". se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
end

program table_std_het
syntax, filename(name)
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Adjusted difference   HIM""Adjusted difference   HIM+FE"  "P-value   control vs. HIM" "P-value   control vs. HIM+FE" "P-value   HIM vs. HIM+FE""N") cells(" beta_2(fmt(3) star pvalue(pval_1_2)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_2(fmt(3)) pval_1_3(fmt(3)) pval_2_3(fmt(3)) Num(fmt(0))" " se_2(fmt(3) par) se_3(fmt(3) par) . . . ." ) 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Adjusted difference - HIM" "Adjusted difference - HIM+FE"  "P-value - control vs. HIM"  "P-value - control vs. HIM+FE"  "P-value - HIM vs. HIM+FE" "N") cells( "beta_2(fmt(3) star pvalue(pval_1_2)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_2(fmt(3)) pval_1_3(fmt(3)) pval_2_3(fmt(3)) Num(fmt(0))" ) 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel( "Adjusted difference - HIM" "Adjusted difference - HIM+FE"  "P-value - control vs. HIM"  "P-value - control vs. HIM+FE"  "P-value - HIM vs. HIM+FE" "N") cells(" beta_2(fmt(3) star pvalue(pval_1_2)) beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_2(fmt(3)) pval_1_3(fmt(3)) pval_2_3(fmt(3)) Num(fmt(0))" ) 
end

program table_std_het_raq
syntax, filename(name)
esttab using "$output/`filename'.tex",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value"  "N") cells(" beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" " se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.rtf",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells(" beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" " se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
esttab using "$output/`filename'.csv",  nomtitles starlevels(* 0.1 ** 0.05 *** 0.01 )  replace noobs gaps nolines nonumbers label collabel(  "Adjusted difference   HIM-Control" "p-value" "Adjusted difference   HIM+FE-Control" "p-value" "Adjusted difference   HIM+FE-HIM" "p-value" "N") cells(" beta_2(fmt(3) star pvalue(pval_1_2)) pval_1_2 beta_3(fmt(3)star pvalue(pval_1_3)) pval_1_3(fmt(3)) beta_23(fmt(3) star pvalue(pval_2_3)) pval_2_3(fmt(3)) Num(fmt(0))" " se_2(fmt(3) par) . se_3(fmt(3) par) . se_23(par) . ." ) 
end
*/
* PROGRAM TO REMOVE AGE EFFECTS (RELATIVE TO CONTROL) AND INTERNALLY STANDARDISE
********************************************************************************
********************************************************************************

program standardise
syntax varlist, itt2(varname) itt3(varname) age(varname) tester(varname)
	foreach x of local varlist {
		di "`x'"
		
		* age and tester standardised
		regress `x' i.`tester'
		tempvar temp3_`x'
		predict `temp3_`x'', res	
		tempvar temp_`x'
		lowess `temp3_`x'' `age' if `itt2'==0 & `itt3'==0, generate(`temp_`x'')
		tempvar age_mean_`x'
		ipolate `temp_`x'' `age', generate(`age_mean_`x'')
		
		* can create missing vars at top and bottom if the extreme observations are in the treatment group
		set trace on 
		sum `age' if `itt2'==0 & `itt3'==0
		sum `age_mean_`x'' if `age'==r(max)
		replace `age_mean_`x''=r(mean) if `age'>1650 & `age'~=. & `age_mean_`x''==.

		sum `age' if `itt2'==0 & `itt3'==0
		sum `age_mean_`x'' if `age'==r(min)
		replace `age_mean_`x''=r(mean) if `age'<1200 & `age'~=. & `age_mean_`x''==.
		
		set trace off
		
		tempvar as_`x'
		gen `as_`x''=`temp3_`x''-`age_mean_`x''
		tempvar resid2_as_`x'
		gen `resid2_as_`x''=`as_`x''^2
		tempvar temp2_`x'
		lowess  `resid2_as_`x'' `age' if `itt2'==0 & `itt3'==0, generate(`temp2_`x'') bwidth(1.6)
		tempvar var_`x'
		ipolate `temp2_`x'' `age', generate(`var_`x'')
		
		set trace on
		* can create missing vars at top and bottom if the extreme observations are in the treatment group
		sum `age' if `itt2'==0 & `itt3'==0
		sum `var_`x'' if `age'==r(max)
		replace `var_`x''=r(mean) if `age'>1650 & `age'~=. & `var_`x''==.

		sum `age' if `itt2'==0 & `itt3'==0
		sum `var_`x'' if `age'==r(min)
		replace `var_`x''=r(mean) if `age'<1200 & `age'~=. & `var_`x''==.
		set trace off
		
		gen `x'_ats_z=(`as_`x'')/((`var_`x'')^0.5)
		sum `x'_ats_z if `itt2'==0 & `itt3'==0 
		replace `x'_ats_z=(`x'_ats_z-r(mean))/r(sd)
		
		* age standardised
		tempvar temp_`x'
		lowess `x' `age' if `itt2'==0 & `itt3'==0, generate(`temp_`x'')
		tempvar age_mean_`x'
		ipolate `temp_`x'' `age', generate(`age_mean_`x'')
		
				* can create missing vars at top and bottom if the extreme observations are in the treatment group
		sum `age' if `itt2'==0 & `itt3'==0
		sum `age_mean_`x'' if `age'==r(max)
		replace `age_mean_`x''=r(mean) if `age'>1650 & `age'~=. & `age_mean_`x''==.

		sum `age' if `itt2'==0 & `itt3'==0
		sum `age_mean_`x'' if `age'==r(min)
		replace `age_mean_`x''=r(mean) if `age'<1200 & `age'~=. & `age_mean_`x''==.
		
		tempvar as_`x'
		gen `as_`x''=`x'-`age_mean_`x''
		tempvar resid2_as_`x'
		gen `resid2_as_`x''=`as_`x''^2
		tempvar temp2_`x'
		lowess  `resid2_as_`x'' `age' if `itt2'==0 & `itt3'==0, generate(`temp2_`x'') bwidth(1.6)
		tempvar var_`x'
		ipolate `temp2_`x'' `age', generate(`var_`x'')
		
		* can create missing vars at top and bottom if the extreme observations are in the treatment group
		sum `age' if `itt2'==0 & `itt3'==0
		sum `age_mean_`x'' if `age'==r(max)
		replace `var_`x''=r(mean) if `age'>1650 & `age'~=. & `var_`x''==.

		sum `age' if `itt2'==0 & `itt3'==0
		sum `age_mean_`x'' if `age'==r(min)
		replace `var_`x''=r(mean) if `age'<1200 & `age'~=. & `var_`x''==.
		
		gen `x'_as_z=(`as_`x'')/((`var_`x'')^0.5)
		sum `x'_as_z if `itt2'==0 & `itt3'==0 
		replace `x'_as_z=(`x'_as_z-r(mean))/r(sd)
		}
end

program standardise_bl // standardise by whole sample - not just controls
syntax varlist,  age(varname) tester(varname)
	foreach x of local varlist {
		regress `x' i.`tester'
		tempvar temp3_`x'
		predict `temp3_`x'', res	
		di "`x'"
		tempvar age_mean_`x'
		lowess `temp3_`x'' `age', generate(`age_mean_`x'')
		tempvar as_`x'
		gen `as_`x''=`temp3_`x''-`age_mean_`x''
		tempvar resid2_as_`x'
		gen `resid2_as_`x''=`as_`x''^2
		tempvar var_`x'
		lowess  `resid2_as_`x'' `age', generate(`var_`x'') bwidth(1.6)
		gen `x'_ats_z=(`as_`x'')/((`var_`x'')^0.5)
		sum `x'_ats_z 
		replace `x'_ats_z=(`x'_ats_z-r(mean))/r(sd)	
		
				* age standardised
		tempvar age_mean_`x'
		lowess `x' `age', generate(`age_mean_`x'')
		*ipolate `temp_`x'' `age', generate(`age_mean_`x'')
		tempvar as_`x'
		gen `as_`x''=`x'-`age_mean_`x''
		tempvar resid2_as_`x'
		gen `resid2_as_`x''=`as_`x''^2
		tempvar var_`x'
		lowess  `resid2_as_`x'' `age' , generate(`var_`x'') bwidth(1.6)
		gen `x'_as_z=(`as_`x'')/((`var_`x'')^0.5)
		sum `x'_as_z 
		replace `x'_as_z=(`x'_as_z-r(mean))/r(sd)
		
		}
end



		
program myRWpvalues_setup_alt, eclass // program when control for that specifc measure at BL
     version 8
     syntax varlist [if] [in], b(integer) setname(name) itt2(varname) itt3(varname) sided(integer) [bl(name) controls(varlist) cluster(varname) ]
     tempname keep V
	
	preserve
	svyset, psu(`cluster')
	
	
	cap drop miss_*
	* replace control variables with mean if missing and generate dummy
		foreach x of varlist  `controls'  {
				count if `x'~=.
				di r(N)
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				gen miss_cont_`x'=(`x'==.)
				if `N'~=`n0'+`n1' {
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				}

	foreach var of local varlist {	
			local names1 "`names1' b_`var'_itt2 se_`var'_itt2  b_`var'_itt3 se_`var'_itt3 b_`var'_itt23 se_`var'_itt23 "
			}
			
     foreach var of local varlist {
		di "`var'"
				
	
		if "`bl'"=="" {
			di "no bl equivalent exists"	
			regress `var'  `itt2' `itt3' `controls' miss_cont_*  dane_*, cluster(`cluster')
			}
		
		else {
		di "bl variable exists"

			foreach x of varlist  `var'_0 {
				count if `x'~=.
				local N=r(N)
				count if `x'==1
				local n1=r(N)
				count if `x'==0
				local n0=r(N)
				if `N'~=`n0'+`n1' {
					gen miss_`x'=(`x'==.)
					sum `x'
					replace `x'=r(mean) if `x'==. 
					}
				}
				
			* run regression
			regress `var'  `itt2' `itt3' `var'_0 `controls' miss_cont* miss_`var'_0  dane_*, cluster(`cluster')
			}	
			
		mat `V'=e(V)
		* save actual beta and SE into memory
		local b_`var'_itt2=_b[`itt2']
		local b_`var'_itt3=_b[`itt3']
		local b_`var'_itt23=_b[`itt3']-_b[`itt2']
		
		local myb_`var'_itt2=_b[`itt2']
		local myb_`var'_itt3=_b[`itt3']
		local myb_`var'_itt23=_b[`itt3']-_b[`itt2']
		
		local se_`var'_itt2=_se[`itt2']
		local se_`var'_itt3=_se[`itt3']
		local se_`var'_itt23=(`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
		*local t_`var'_itt2=_b[`itt2']/_se[`itt2']   // NB FOR TWO TAILED TEST THIS WOULD BE ABSOLUTE VALUE!!!!!
		*local t_`var'_itt3=_b[`itt3']/_se[`itt3']
		*local  t_`var'_itt23 =(_b[`itt3']-_b[`itt2'])/((`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5) // abs here if two tailed
		local names2 "`names2' (`b_`var'_itt2') (`se_`var'_itt2')  (`b_`var'_itt3') (`se_`var'_itt3')  (`b_`var'_itt23') (`se_`var'_itt23') "
		}
		
		tempfile temp_rw2
		tempname rw_actual
		postfile `rw_actual' `names1' using `temp_rw2', replace

		post `rw_actual'  `names2'
					
		postclose `rw_actual'
		
			* just keep the vars we need
		tempfile keep
		keep `varlist' `controls'  dane_* *0 `itt2' `itt3' `cluster' miss_cont*
		save `keep', replace
	
		* bootstrap
		*local B = 1000  /* NUMBER OF REPLICATIONS*/
		g n = _n
		set seed 1
		tempname rw
		tempfile temp_rw

		/*
		foreach var of local varlist {	
			local names1 "`names1' t_`var'"
			}
			*/
		postfile `rw' iter `names1' using `temp_rw', replace

		* BOOTSTRAP LOOP 
		local I = 1
		while `I' <= `b' {
			di as error "hello" _cont
			*qui {
				use `keep', clear
				local names2 ""
								
				tempvar id_cluster
				bsample , cluster(`cluster') idcluster(`id_cluster')	
				
				*tab `id_cluster'
				local counter = 1
				foreach var of local varlist {	
					
					
					if "`bl'"=="" {
						di "no bl equivalent exists"	
						regress `var'  `itt2' `itt3' `controls' miss_cont_*  dane_* , cluster(`id_cluster')
						}
					else {
							di "bl variable exists"
							regress `var'  `itt2' `itt3' `var'_0 `controls' miss_cont* miss_`var'_0  dane_* , cluster(`id_cluster')
							}	
					tempvar sample		
					gen `sample'=e(sample)
					
					*tab `id_cluster' if `sample'==0
					*tab `cluster' if `sample'==0
					
					mat `V'=e(V)
						local b_`var'_itt2=_b[`itt2']
						local b_`var'_itt3=_b[`itt3']
						local b_`var'_itt23=_b[`itt3']-_b[`itt2']
						local se_`var'_itt2=_se[`itt2']
						local se_`var'_itt3=_se[`itt3']
						local se_`var'_itt23=(`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5
						*local t_`var'_itt2=_b[`itt2']/_se[`itt2']   // NB FOR TWO TAILED TEST THIS WOULD BE ABSOLUTE VALUE!!!!!
						*local t_`var'_itt3=_b[`itt3']/_se[`itt3']
						*local  t_`var'_itt23 =(_b[`itt3']-_b[`itt2'])/((`V'[2,2]+`V'[1,1]-2*`V'[2,1])^0.5) // abs here if two tailed
						local names2 "`names2' (`b_`var'_itt2') (`se_`var'_itt2') (`b_`var'_itt3') (`se_`var'_itt3')  (`b_`var'_itt23') (`se_`var'_itt23') "
						}
		
					
				di "`names2'"
				*mat list `V'
				post `rw' (`I')  `names2'
				local counter = `counter' + 1
			local I = `I' + 1
		} /*END BOOTSTRAP (WHILE) LOOP*/
		
		postclose `rw'
		use `temp_rw', clear
		
		* calc new t-stats
		*rename t_* t_*_error
		
		* bootstrapped ses
		
		
		foreach var of local varlist {
			sum b_`var'_itt2
			*gen t_`var'_itt2 =(b_`var'_itt2-r(mean))/r(sd)
			*gen t_`var'_itt2=(b_`var'_itt2-r(mean))/se_`var'_itt2
			gen t_`var'_itt2=(b_`var'_itt2-`myb_`var'_itt2')/se_`var'_itt2
			
			
			
			sum b_`var'_itt3
			*gen t_`var'_itt3 =(b_`var'_itt3-r(mean))/r(sd)
			*gen t_`var'_itt3=(b_`var'_itt3-r(mean))/se_`var'_itt3
			gen t_`var'_itt3=(b_`var'_itt3-`myb_`var'_itt3')/se_`var'_itt3
			
			sum b_`var'_itt23
			*gen t_`var'_itt23 =(b_`var'_itt23-r(mean))/r(sd)
			*gen t_`var'_itt23=(b_`var'_itt23-r(mean))/se_`var'_itt23
			gen t_`var'_itt23=(b_`var'_itt23-`myb_`var'_itt23')/se_`var'_itt23
			
			
			}
		
		drop iter
		
		* replace with absolute values for twotailed test 
		if "`sided'"=="2" {
			foreach x of varlist t_* {
				replace `x'=abs(`x')
				}
			}
		
		save `temp_rw', replace
		
		* export as three files for itt2, itt3 and itt23
		use `temp_rw', clear
		
		export delimited using  "C:\Users\alison_a\Dropbox\HI_shared (1)\data\RW\T-value distributions\NullDist_`setname'_allstats.csv",	replace
		
		keep t_*itt2
		export delimited using  "C:\Users\alison_a\Dropbox\HI_shared (1)\data\RW\T-value distributions\NullDist_`setname'_itt2.csv",	replace
		
		use `temp_rw', clear
		keep t_*itt3
		export delimited using  "C:\Users\alison_a\Dropbox\HI_shared (1)\data\RW\T-value distributions\NullDist_`setname'_itt3.csv",	replace
		
		use `temp_rw', clear
		keep t_*itt23
		export delimited using  "C:\Users\alison_a\Dropbox\HI_shared (1)\data\RW\T-value distributions\NullDist_`setname'_itt23.csv",	replace
		
		use `temp_rw2', clear
		* generate t values
		foreach var of local varlist {
			gen t_`var'_itt2 =(b_`var'_itt2)/se_`var'_itt2
			gen t_`var'_itt3 =(b_`var'_itt3)/se_`var'_itt3
			gen t_`var'_itt23 =(b_`var'_itt23)/se_`var'_itt23		
			}
		keep t_*itt2
			* replace with absolute values for twotailed test 
		if "`sided'"=="2" {
			foreach x of varlist t_* {
				replace `x'=abs(`x')
				}
			}
		
		export delimited using  "C:\Users\alison_a\Dropbox\HI_shared (1)\data\RW\T-value distributions\ActualTs_`setname'_itt2.csv",	replace

		use `temp_rw2', clear
		* generate t values
		foreach var of local varlist {
			gen t_`var'_itt2 =(b_`var'_itt2)/se_`var'_itt2
			gen t_`var'_itt3 =(b_`var'_itt3)/se_`var'_itt3
			gen t_`var'_itt23 =(b_`var'_itt23)/se_`var'_itt23	
			}		
		keep t_*itt3
				* replace with absolute values for twotailed test 
		if "`sided'"=="2" {
			foreach x of varlist t_* {
				replace `x'=abs(`x')
				}
			}
		
		export delimited using  "C:\Users\alison_a\Dropbox\HI_shared (1)\data\RW\T-value distributions\ActualTs_`setname'_itt3.csv",	replace

		use `temp_rw2', clear
		* generate t values
		foreach var of local varlist {
			gen t_`var'_itt2 =(b_`var'_itt2)/se_`var'_itt2
			gen t_`var'_itt3 =(b_`var'_itt3)/se_`var'_itt3
			gen t_`var'_itt23 =(b_`var'_itt23)/se_`var'_itt23	
			}		
		keep t_*itt23
				* replace with absolute values for twotailed test 
		if "`sided'"=="2" {
			foreach x of varlist t_* {
				replace `x'=abs(`x')
				}
			}
		
		export delimited using  "C:\Users\alison_a\Dropbox\HI_shared (1)\data\RW\T-value distributions\ActualTs_`setname'_itt23.csv",	replace

		use `temp_rw', clear

		restore
		end
		
		
		
		program myRWpvalues_return, eclass
* import and return as e() class
version 8
syntax varlist [if] [in], setname(name) 
preserve
tempname RW_pvalue_2  RW_pvalue_3 RW_pvalue_23
import delimited using  "$rw/RW_Pvalues_`setname'_itt2.csv",	clear
count
local N=r(N)
foreach i of numlist 1/`N' {
	sum v1 in `i'
	mat `RW_pvalue_2'	=nullmat(`RW_pvalue_2'), r(mean)
	}
	mat list `RW_pvalue_2'

import delimited using  "$rw/RW_Pvalues_`setname'_itt3.csv", clear
foreach i of numlist 1/`N' {
	sum v1 in `i'
	mat `RW_pvalue_3'	=nullmat(`RW_pvalue_3'), r(mean)
	}
	mat list `RW_pvalue_3'

import delimited using  "$rw/RW_Pvalues_`setname'_itt23.csv", clear
foreach i of numlist 1/`N' {
	sum v1 in `i'
	mat `RW_pvalue_23'	=nullmat(`RW_pvalue_23'), r(mean)
	}
	mat list `RW_pvalue_23'
	
foreach mat in  RW_pvalue_2 RW_pvalue_3 RW_pvalue_23  {
			di "`mat'"
			mat list ``mat''
			mat colname ``mat'' = `varlist'
			}
		
 foreach mat in  RW_pvalue_2 RW_pvalue_3 RW_pvalue_23   {
			 eret mat `mat' = ``mat''	
			 }

restore
end	

program mySIDAKpvalues_return, eclass
		* import and return as e() class
		version 8
		syntax namelist [if] [in]
		tempname RW_pvalue_2  RW_pvalue_3 RW_pvalue_23

		count
		local N=r(N)
		foreach i of numlist 1/`N' {
			sum itt2_sidak_2s in `i'
			mat `RW_pvalue_2'	=nullmat(`RW_pvalue_2'), r(mean)
			}
			mat list `RW_pvalue_2'

		foreach i of numlist 1/`N' {
			sum itt3_sidak_2s in `i'
			mat `RW_pvalue_3'	=nullmat(`RW_pvalue_3'), r(mean)
			}
			mat list `RW_pvalue_3'

		foreach i of numlist 1/`N' {
			sum itt23_sidak_2s in `i'
			mat `RW_pvalue_23'	=nullmat(`RW_pvalue_23'), r(mean)
			}
			mat list `RW_pvalue_23'
			
		foreach mat in  RW_pvalue_2 RW_pvalue_3 RW_pvalue_23  {
					di "`mat'"
					mat list ``mat''
					mat colname ``mat'' = `namelist'
					}
				
		 foreach mat in  RW_pvalue_2 RW_pvalue_3 RW_pvalue_23   {
					 eret mat `mat' = ``mat''	
					 }
			
		end	
		
